package com.ruoyi.ams.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.ams.domain.vo.ThisYearAssetChangeVo;
import com.ruoyi.ams.mapper.AmsAssetInfoChangeMapper;
import com.ruoyi.ams.service.AmsReusingService;
import com.ruoyi.ams.service.IAmsDataStatisticsService;
import  com.ruoyi.asms.service.IAsmsAssetFullInfoService;
import com.ruoyi.common.core.domain.AsmsAssetFullInfo;
import com.ruoyi.common.core.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class AmsDataStatisticsServiceImpl implements IAmsDataStatisticsService {

    @Autowired
    private AmsAssetInfoChangeMapper assetInfoChangeMapper;
    @Autowired
    private IAsmsAssetFullInfoService asmsAssetFullInfoService;
    @Autowired
    private AmsReusingService amsReusingService;

    @Override
    public List<List<ThisYearAssetChangeVo>> thisYearAssetChange() {
        //先获取当前维护公司维护的资产
        List<AsmsAssetFullInfo> assets = asmsAssetFullInfoService
                .list(new QueryWrapper<AsmsAssetFullInfo>().select(AsmsAssetFullInfo.ASSET_ID)
                .eq(AsmsAssetFullInfo.MAINTENANCE_DEPT_ID, SecurityUtils.getDeptId()));
        StringBuilder assetIdsStr = new StringBuilder();
        int flag = 0;
        for (AsmsAssetFullInfo asset : assets) {
            flag ++;
            if (asset != null && asset.getAssetId() != null) {
                assetIdsStr.append(asset.getAssetId().toString());
                if (flag != assets.size())
                    assetIdsStr.append(",");
            }
        }
        //获取资产变动情况
        Long lastYearStartTime = amsReusingService.getLastYearStartTime();
        Long thisYearStartTime = amsReusingService.getThisYearStartTime();
        //获取去年的资产变动情况
        List<ThisYearAssetChangeVo> lastYear = assetInfoChangeMapper
                .thisYearAssetChange(assetIdsStr.toString(), lastYearStartTime, thisYearStartTime);
        //获取今年的资产变动情况
        List<ThisYearAssetChangeVo> thisYear = assetInfoChangeMapper
                .thisYearAssetChange(assetIdsStr.toString(), thisYearStartTime, System.currentTimeMillis());

        HashMap<String, Integer> lastMap = new HashMap<>();
        HashMap<String, Integer> thisMap = new HashMap<>();

        lastYear.forEach(item -> lastMap.put(item.getChangeMonth(), item.getNum()));
        thisYear.forEach(item -> thisMap.put(item.getChangeMonth(), item.getNum()));

        String[] months =  {"一月", "二月", "三月", "四月",
                            "五月", "六月", "七月", "八月",
                            "九月", "十月", "十一月", "十二月"};

        List<List<ThisYearAssetChangeVo>> result = new ArrayList<>();
        List<ThisYearAssetChangeVo> lastYearResult = new ArrayList<>();
        List<ThisYearAssetChangeVo> thisYearResult = new ArrayList<>();

        for (int i = 0; i < months.length; i ++) {
            String index = "";
            int j = i + 1;
            if (i < 9)
                index = "0" + j;
            else
                index = "" + j;

            ThisYearAssetChangeVo vo1 = new ThisYearAssetChangeVo();
            vo1.setChangeMonth(months[i]);
            vo1.setNum(lastMap.get(index) != null ? lastMap.get(index) : 0);
            lastYearResult.add(vo1);

            ThisYearAssetChangeVo vo2 = new ThisYearAssetChangeVo();
            vo2.setChangeMonth(months[i]);
            vo2.setNum(thisMap.get(index) != null ? thisMap.get(index) : 0);
            thisYearResult.add(vo2);
        }
        result.add(lastYearResult);
        result.add(thisYearResult);
        return result;
    }
}
